/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 *
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl-3.0.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.model.entity;

import xperiment.metaphor.model.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import metaphor.service.ServiceException;
import org.apache.commons.lang.builder.CompareToBuilder;

/**
 * Department
 *
 * @author Mark Ashworth<javameme@gmail.com>
 * @version 1.0.0
 * @since 1.0.0
 */
//@Entity
//@Table(name="department")
public class Department extends DomainObject implements Persistent, Named, Comparable, XML {
    private static final long serialVersionUID = 1L;
    
    /* The unique database id for the object */
    //@Id @GeneratedValue(strategy=GenerationType.AUTO)
    //@Column(name = "department_id", unique = true, nullable = false)
    private Long id = null;
    
    /* The name of the department */
    //@Column(name="department_name", unique=false, nullable=false)
    private String name;
    
    /* Whether the department is enabled */
    //@Column(name = "enabled", nullable = false)
    private boolean enabled = true;

    /**
     * Default constructor
     */
    public Department() {
        this(null);
    }
    /**
     * Constructs the department
     * @param name The name of the department
     */
    public Department(String name) {
        super();
        this.name = name;
    }

    /**
     * The unique database id for the object
     * @return Long
     */
    @Override
    public Long getId() {
        return this.id;
    }
    
    /**
     * The name of the department
     * @return String
     */
    @Override
    public String getName() {
        return this.name;
    }

    /**
     * The name of the department
     * @param name The new value
     */
    public void setName(String name) {
        String old = getName();
        this.name = name;
        firePropertyChange("name", old, getName());
    }

    /**
     * Whether the department is enabled
     * @return boolean
     */
    public boolean isEnabled() {
        return this.enabled;
    }
    
    /**
     * Whether the department is enabled
     * @param enabled The new value
     */
    public void setEnabled(boolean enabled) {
        boolean old = isEnabled();
        this.enabled = enabled;
        firePropertyChange("enabled", old, isEnabled());
    }

    /**
     * Saves the department to the persistent store
     * @throws xperiment.metaphor.model.ModelException If there was an error
     */
    public Department save() throws ModelException {

        try {
            Department d = (Department) getDao().persist(this);
            this.id = d.getId();
            return this;
        } catch (ServiceException e) {
            throw new SaveException("Unable to save department", e);
        }
    }

    /**
     * Sets the departments's name
     * @param name The new value
     * @return Department
     */
    public Department name(String name) {
        this.setName(name);
        return this;
    }

    /**
     * Returns the name as the String representation of the department
     * @return String
     */
    @Override
    public String toString() {
        return getName();
    }
    
    /**
     * Returns the XML representation of the department
     * &lt;department class="xperiment.metaphor.model.organization.Department" id="1" name="IT" enabled="true" /&gt;<br />
     * @return String
     */
    public String toXML() {
        return "<department class='" 
                + getClass().getName() 
                + "' id='" + getId()
                + "' name='" + getName()
                + "' enabled='" + isEnabled()
                + "' />";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Department other = (Department) obj;
        if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }
    
    @Override
    public int compareTo(Object o) {
        Department rhs = (Department) o;
        return new CompareToBuilder()
                .append(this.getName(), rhs.getName())
                .toComparison();
	}
}
